const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const path = require('path');
const HappyPack = require('happypack');
const os = require('os');

const globalConfig = require('./config');
const webpack = require('webpack');


module.exports = function (calcConfig, { type, html }) {
  

    return {
        entry: {
            [calcConfig.moduleName]: ['regenerator-runtime/runtime', path.join(calcConfig.modulePath, globalConfig.entry)]
        },
        module: {
            rules: [
                {
                    //     test: /\.js$/,
                    //     use: 'eslint-loader',
                    //     enforce: 'pre'
                    // }, {
                    test: /\.j|tsx?$/,
                    use: ['happypack/loader?id=happyBabel'],
                    exclude: /node_modules/
                }, {
                    test: /\.(png)|(jpe?g)|(gif)/i,
                    use: [
                        {
                            loader: 'url-loader',
                            options: {
                                limit: 1024 * 50,
                                esModule: false,
                                name: `${globalConfig.prefix.image}/[name].[hash:7].[ext]`,
                                /*publicPath: './'*/
                            }
                        }
                    ]
                }, {
                    test: /\.(eot|otf|mp3|mp4)$/,
                    loader: 'file-loader',
                    options: {
                        name: `${globalConfig.prefix.resource}/[name].[hash:7].[ext]`,
                        publicPath: './../../',
                        esModule: false
                    }
                }, {
                  test: /\.(woff|woff2|ttf)$/,
                  loader: 'file-loader',
                  options: {
                      name: `${globalConfig.prefix.resource}/[name].[hash:7].[ext]`,
                      publicPath: process.env.NODE_ENV === 'development'? './../../':'./../',
                      //原路径是./../../  现在由因为生成字体路径因为多项目框架导致上一个层级没有明确路径
                      esModule: false
                  }
              }]
        },
        resolve: {
            // 局部@, 全局@@
            alias: {
                '@@': calcConfig.srcPath,
                '@@styles': path.join(calcConfig.srcPath, 'common/styles'),
                '@@plugins': path.join(calcConfig.srcPath, 'common/plugins'),
                '@@components': path.join(calcConfig.srcPath, 'common/components')
            },
            // modules: [ path.resolve(calcConfig.basePath, 'node_modules') ],
            extensions: ['.js']
        },
        optimization: {
            splitChunks: {
                chunks: "async",
                minSize: 30000,
                minChunks: 1,
                maxAsyncRequests: 5,
                maxInitialRequests: 3,
                automaticNameDelimiter: '~',
                name: true,
                cacheGroups: {
                    vendor: {
                        test: /[\\/]node_modules[\\/]/,
                        name: "vendor",
                        chunks: "all"
                    },
                    // videojs: {
                    //     test: /video\.js/,
                    //     name: "videojs",
                    //     priority: -10
                    // }
                }
            },
            runtimeChunk: {
                name: 'manifest'
            }
        },
        plugins: [
            new webpack.NamedModulesPlugin(),
            new webpack.HashedModuleIdsPlugin(),
            new HtmlWebpackPlugin({
                title: html.title,
                template: path.join(calcConfig.modulePath, 'index.html'),
                meta: {
                    viewport: globalConfig.html.viewport
                },
                gio: globalConfig.html.gio,
                noDisplayGio: globalConfig.html.noDisplayGio,
                isDisplayLinkedme: html.isDisplayLinkedme,
                dll: [process.env.NODE_ENV === 'development' ? `` /*/${type}.dll.js*/ : `https://hxsapp-oss.hxsapp.com/public/js/vue.dll.js`]
            }),
            new HappyPack({
                id: 'happyBabel',
                loaders: ['babel-loader?cacheDirectory'],
                threads: os.cpus().length
            }),
            new CleanWebpackPlugin({
                cleanOnceBeforeBuildPatterns: ['**/*', '!*.dll.js', '!*-manifest.json'],
            }),
            new LodashModuleReplacementPlugin()
        ]
    }
}
